package com.yuyou.fn.timer.task;


import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;

/**
 *
 * 同步前天的业务数据到报表库
 *
 * Created by wens on 2017/4/26.
 */
@JobHander(value="SyncTableDataTask")
@Component
public class SyncTableDataTask extends IJobHandler {

    @Resource
    private DataSource dataSource ;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        List<String> tableNames  = Arrays.asList("student","course_class","teacher","bus_team","area_team","pay_record","student_refund","change_record","student_reg","campus","class_lecture","new_pay_record");

        Connection connection = null ;
        Statement statement = null ;
        try{
            connection = dataSource.getConnection();
            statement = connection.createStatement();

            for(String tableName : tableNames ){
                statement.execute("update "+tableName+" set update_time=date_add(update_time ,interval 1 second ) where update_time > date_add(update_time ,interval 1 day )");
            }
        }finally {
            if(statement != null ){
                statement.close();
            }
            if(connection != null ){
                connection.close();
            }
        }

        return ReturnT.SUCCESS;
    }
}
